<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>选择工程支付</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>支付明细</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th style="width: 35px!important;padding-left: 0"><input type="checkbox" name="xuanAll" id="xuanAll" lay-filter="xuanAll" lay-skin="primary"></th>
                            <th>流程单号</th>
                            <th>款项类型</th>
                            <th>款项说明</th>
                            <th>付款金额(元)</th>
                            <th>付款对象</th>
                            <th>票据状态</th>
                            <th>累计已收票金额(元)</th>
                            <th>待收票金额(元)</th>
                            <th><text>*</text>本次收票对应付款金额(元)</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div>合计：<span id="alldifee">0</span> 元</div>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var parent_domkey = getQueryVariable('domkey');
    var parent_enginid = getQueryVariable('enginid') || 0;
    var parent_fuer = decodeURI(getQueryVariable('fuer') || '') || '';



    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var difeeText = $("#list_list tr", parent.document).eq(parent_domkey).find('.difeeobj').text();
        if(difeeText){
            var difeeobj = JSON.parse(difeeText);
        }else{
            var difeeobj = {};
        }


        function check_exist(data, gongzhiid, gongzhilistid) {
            var difee = '';
            var res = false;
            for (var key in data){
                if(data[key].gongzhiid == gongzhiid && data[key].gongzhilistid == gongzhilistid){
                    difee = data[key].difee;
                    res = true;
                }
            }
            return {difee:difee, res:res};
        }


        facade.ajax({url: ask_gong_zhi_piaoshou_guan_list, data:{fuer:parent_fuer,enginid:parent_enginid}}).done(function(res){
            if (res.code === 2000) {
                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';

                        var existInfo = check_exist(difeeobj, list[key].gongzhiid, list[key].gongzhilistid);
                        if(existInfo.res){
                            str += '<td style="width: 45px!important;"><input type="checkbox" name="xuanhang" lay-filter="xuanhang" class="xuanhang" checked lay-skin="primary"></td>';
                        }else{
                            str += '<td style="width: 45px!important;"><input type="checkbox" name="xuanhang" lay-filter="xuanhang" class="xuanhang" lay-skin="primary"></td>';
                        }
                        str += '<td><input type="text" value="'+list[key].sericnum+'" class="layui-input disabled sericnum" disabled></td>';
                        str += '<td><input type="text" value="'+list[key].zhikuanlei_name+'" class="layui-input disabled zhikuanlei_name" disabled></td>';
                        str += '<td><input type="text" value="'+list[key].explain+'" class="layui-input disabled explain" disabled></td>';
                        str += '<td><input type="number" value="'+list[key].money+'" class="layui-input disabled money" disabled></td>';
                        str += '<td><input type="text" value="'+list[key].fuer+'" class="layui-input disabled fuer" disabled></td>';
                        str += '<td><input type="text" value="'+list[key].statename+'" gongzhilistid="'+list[key].gongzhilistid+'" gongzhiid="'+list[key].gongzhiid+'" class="layui-input disabled statename" disabled></td>';
                        str += '<td><input type="number" value="'+list[key].yifufee+'" class="layui-input disabled yifufee" disabled></td>';
                        str += '<td><input type="number" value="'+list[key].daifufee+'" class="layui-input disabled daifufee" disabled></td>';
                        if(existInfo.res){
                            str += '<td><input type="number" value="'+existInfo.difee+'" class="layui-input difee"></td>';
                        }else{
                            str += '<td><input type="number" value="" class="layui-input difee"></td>';
                        }

                        str += '</tr>';
                        $("#list_list").append(str);
                        update_refresh_row_func()
                    }
                }

                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('checkbox(xuanAll)', function(data){
            if(this.checked){
                $('.xuanhang').prop("checked", true);
            }else{
                $(".xuanhang").prop('checked', false);
            }
            render_from();
        });
        form.on('checkbox(xuanhang)', function(data){
            jisuan_alldifee_all();
        });


        form.on('submit(add)', function(data){
            var list = [];
            var alldifee = 0;
            try {
                $('#list_list tr').each(function (i) {
                    var checked = $(this).find('.xuanhang').prop("checked");
                    if(checked){
                        var gongzhiid = $(this).find('.statename').attr('gongzhiid');
                        var gongzhilistid = $(this).find('.statename').attr('gongzhilistid');
                        var daifufee = $(this).find('.daifufee').val();
                        var difee = $(this).find('.difee').val() || 0;

                        var index = i + 1;
                        //只能填写大于0
                        if(difee == ''){
                            throw new Error('第'+index+'行，请输入本次收票对应付款金额');
                        }
                        if(parseFloat(difee) > parseFloat(daifufee)){
                            throw new Error('第'+index+'行，本次支付占用进项票金额已大于待付款金额');
                        }
                        alldifee = parseFloat(alldifee) + parseFloat(difee);
                        list.push({
                            gongzhiid:gongzhiid,
                            gongzhilistid:gongzhilistid,
                            difee:difee,
                        });
                    }
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();


            var datastr = JSON.stringify(data);


            $("#list_list tr", parent.document).eq(parent_domkey).find('.difee').val(alldifee);
            $("#list_list tr", parent.document).eq(parent_domkey).find('.difeeobj').html(JSON.stringify(list));
            layer_close();
            return false;
        });

    });





    $('#list_list').on('input propertychange','.difee',function () {
        jisuan_alldifee_all();
    });

    function jisuan_alldifee_all() {
        var alldifee = 0;
        $('#list_list tr').each(function (i) {
            var checked = $(this).find('.xuanhang').prop("checked");
            if(checked){
                var difee = parseValidNum($(this).find('.difee').val());
                alldifee = parseFloat(alldifee) + parseFloat(difee);
            }
        });
        $("#alldifee").text(parseFormatNum(alldifee,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_alldifee_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }
</script>
</body>
</html>
